package com.huiren.ui.importUI;

import javax.annotation.PostConstruct;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.JTable;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.huiren.bean.Order;
import com.huiren.meta.SearchMeta;
import com.huiren.service.OrderService;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

@Component
public class DataSearchJPanel extends JPanel {
	public DataSearchJPanel() {

	}

	private static Logger logger = Logger.getLogger(DataSearchJPanel.class);
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTextField receiptVal;
	private JTextField warhouse_idVal;
	private JTable table = null;
	private JComboBox comboBox;
	private DateTableModel model;
	@Autowired
	private OrderService orderService;

	@PostConstruct
	public void initMethod() {
		setLayout(new BorderLayout(0, 0));

		JPanel panel = new JPanel();
		add(panel, BorderLayout.NORTH);
		JLabel receipt = new JLabel("单据号：");
		panel.add(receipt);

		receiptVal = new JTextField();
		panel.add(receiptVal);
		receiptVal.setColumns(20);

		JLabel warhouse = new JLabel("药库号:");
		panel.add(warhouse);

		warhouse_idVal = new JTextField();
		panel.add(warhouse_idVal);
		warhouse_idVal.setColumns(20);

		comboBox = new JComboBox();
		panel.add(comboBox);
		comboBox.setModel(new DefaultComboBoxModel(new String[] { "所有", "未导入",
				"已导入" }));

		JButton search = new JButton("查询");
		panel.add(search);
		search.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				SearchMeta searchMeta = new SearchMeta();
				searchMeta.setWarhouse_id(warhouse_idVal.getText());
				if (comboBox.getSelectedIndex() == 0)
					searchMeta.setIsok(null);
				else if (comboBox.getSelectedIndex() == 1)
					searchMeta.setIsok(0);
				else if (comboBox.getSelectedIndex() == 2)
					searchMeta.setIsok(1);
				searchMeta.setPlan_code(receiptVal.getText());
				try {
					List<Order> orderList = orderService
							.selectOrder(searchMeta);
					model.setOrderList(orderList);
					table.validate();
					table.updateUI();
				} catch (Exception e) {
					logger.error(e);
				}

			}
		});
		model = new DateTableModel();
		table = new JTable(model);
		table.getColumnModel().getColumn(2).setPreferredWidth(230);
		JScrollPane scrollPane = new JScrollPane(table);
		this.add(scrollPane, BorderLayout.CENTER);
	}
}
